package com.pro01.backend.controller;

import com.pro01.backend.pojo.Notification;
import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.AdminNotificationService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/notifications")
public class AdminNotificationController {
    @Autowired
    private AdminNotificationService adminNotificationService;

    @PostMapping
    public Result<Void> sendSystemNotification(@RequestBody Map<String, String> requestBody,
                                               HttpServletRequest request) {
        try {
            String role = TokenUtil.getRole(request);
            if (!"ADMIN".equals(role)) {
                return Result.error("无权限，只有管理员可以发送系统通知");
            }

            String content = requestBody.get("content");
            String link = requestBody.get("link");

            if (content == null || content.trim().isEmpty()) {
                return Result.error("通知内容不能为空");
            }

            adminNotificationService.sendSystemNotification(content, link);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发送系统通知失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询所有系统通知
     */
    @GetMapping("/system")
    public Result<PageResult<Notification>> getSystemNotifications(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        try {
            String role = TokenUtil.getRole(request);
            if (!"ADMIN".equals(role)) {
                return Result.error("无权限，只有管理员可以查看系统通知记录");
            }

            PageResult<Notification> pageResult = adminNotificationService.getSystemNotifications(pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询系统通知失败：" + e.getMessage());
        }
    }

    /**
     * 删除一条系统通知
     */
    @DeleteMapping("/system")
    public Result<Void> deleteSystemNotification(@RequestBody Map<String, String> requestBody,
                                                 HttpServletRequest request) {
        try {
            String role = TokenUtil.getRole(request);
            if (!"ADMIN".equals(role)) {
                return Result.error("无权限，只有管理员可以删除系统通知");
            }

            String content = requestBody.get("content");
            String link = requestBody.get("link");

            if (content == null || content.trim().isEmpty()) {
                return Result.error("通知内容不能为空");
            }

            adminNotificationService.deleteSystemNotification(content, link);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除系统通知失败：" + e.getMessage());
        }
    }
}
